package com.info.zhiduoduo.admin.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.info.zhiduoduo.admin.model.formbean.complaint.AddCompliantFormBean;
import com.info.zhiduoduo.admin.model.formbean.complaint.ComplaintListFormBean;
import com.info.zhiduoduo.admin.model.formbean.complaint.ReplyComplaintFormBean;
import com.info.zhiduoduo.common.constants.SmartResult;
import com.info.zhiduoduo.common.constants.StatusMsg;
import com.info.zhiduoduo.common.formbean.BaseSimpleFormBean;
import com.info.zhiduoduo.core.service.common.BaseService;
import com.info.zhiduoduo.repository.dto.complaint.ComplaintDto;
import com.info.zhiduoduo.repository.entity.complaint.Complaint;
import com.info.zhiduoduo.repository.mapper.maker.ComplaintMapper;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

import javax.validation.Valid;

@Service
public class ComplaintService extends BaseService {

	@Autowired
	ComplaintMapper complaintMapper;

	public SmartResult<ComplaintDto> getComplaintList(BaseSimpleFormBean<ComplaintListFormBean> formBean) {
		@Valid ComplaintListFormBean reqdata = formBean.getReqdata();
		if (reqdata == null) return errorParamSmartResult();

		int userType = reqdata.getUserType();
		String name = reqdata.getName();
		Integer needReply = reqdata.getNeedReply();
		Integer hasReply = reqdata.getHasReply();

		Page<ComplaintDto> page = PageHelper.startPage(formBean.getPage(), formBean.getRows());
		complaintMapper.getList(userType, name, needReply, hasReply);

		return smartResultPage(page);
	}

	public SmartResult replyComplaint(BaseSimpleFormBean<ReplyComplaintFormBean> formBean) {
		@Valid ReplyComplaintFormBean reqdata = formBean.getReqdata();
		if (reqdata == null) return errorParamSmartResult();

		Complaint complaint = new Complaint().selectById(reqdata.getId());
		if (complaint == null) return smartResult(StatusMsg.StatusMsg_801);

		complaint.setReplyContent(reqdata.getReplyContent())
				.setReplyManagerId(formBean.getUserid())
				.setReplytime(new Date())
				.setHasReply(1);
		if (complaint.updateById()) return smartResult();
		return optFailSmartResult();
	}

	public SmartResult<ComplaintDto> getDetail(BaseSimpleFormBean<Integer> formBean) {
		@Valid Integer id = formBean.getReqdata();
		ComplaintDto complaintDto = complaintMapper.getDetail(id);
		if (complaintDto == null) return smartResult(StatusMsg.StatusMsg_801);
		return smartResult(complaintDto);
	}
	public SmartResult addOrUpdateComplaint(BaseSimpleFormBean<AddCompliantFormBean> formBean){
		if(formBean==null || formBean.getReqdata()==null){
			return errorParamSmartResult();
		}
		AddCompliantFormBean reqdata = formBean.getReqdata();
		Complaint complaint = new Complaint();
		BeanUtils.copyProperties(reqdata,complaint);
		complaint.setAddAdminid(formBean.getUserid());
		if(!complaint.insertOrUpdate()){
			return smartResult(StatusMsg.StatusMsg_199);
		}

		return smartResult(complaint);
	}
}
